package com.ysm.smfu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.bean.City;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * ====================
 * 版权所有 违法必究
 *
 * @author wangx
 * @project QuickIndexBar
 * @file CityAdapter
 * @create_time 2016/8/30 0030
 * @github https://github.com/wangxujie
 * @blog http://wangxujie.github.io
 * <p/>
 * ======================
 */
public class CityAdapter extends BaseAdapter {
    private Context context;
    private List<City> cityList;

    public CityAdapter(Context context, List<City> cityList) {
        this.context = context;

        this.cityList = cityList;
    }

    @Override
    public int getCount() {
        return cityList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.list_item_city, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        City city = cityList.get(position);//当前对象

//        上一个对象
        City preCity = null;
        if (position == 0) {
            preCity = null;//索引为0的 好汉 上一个好汉 就没有
        } else {
            preCity=  cityList.get(position - 1);
        }

        boolean isShowPinYin = true;
        if (preCity == null){
            isShowPinYin = true;
        }else{
            if (city.pinyin.charAt(0) == preCity.pinyin.charAt(0)){
                isShowPinYin = false;
            }
        }

        holder.tv_pinyin.setVisibility(isShowPinYin?View.VISIBLE:View.GONE);
        holder.tv_name.setText(city.name);
        holder.tv_pinyin.setText(String.valueOf(city.pinyin.charAt(0)));
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public TextView tv_pinyin;
        public TextView tv_name;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_pinyin = (TextView) rootView.findViewById(R.id.tv_pinyin);
            this.tv_name = (TextView) rootView.findViewById(R.id.tv_name);
        }
    }
}
